package com.mt.roll.wms.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mt.roll.core.api.Assert;
import com.mt.roll.core.base.BaseSupport;
import com.mt.roll.core.base.IErrorCode;
import com.mt.roll.core.dal.entity.EquipmentBomDetail;
import com.mt.roll.core.dal.service.EquipmentBomDetailServiceBiz;
import com.mt.roll.core.errorcode.ApiErrorCode;
import com.mt.roll.core.params.IdParam;
import com.mt.roll.core.vo.R;
import com.mt.roll.wms.mapper.EquipmentBomDetailEQMapper;
import com.mt.roll.wms.params.EquipmentBomDetailParam;
import com.mt.roll.wms.params.EquipmentBomDetailQueryParam;
import com.mt.roll.wms.service.EquipmentBomDetailService;
import com.mt.roll.wms.vo.EquipmentBomDetailVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Desc: ""
 * @Author: caixiang
 * @DATE: 2021/1/4 10:12
 */
@Service
@Transactional
public class EquipmentBomDetailServiceImpl extends BaseSupport implements EquipmentBomDetailService {
    @Autowired
    EquipmentBomDetailServiceBiz equipmentBomDetailServiceBiz;

    @Autowired
    EquipmentBomDetailEQMapper equipmentBomDetailEQMapper;

    @Override
    public R<Boolean> create(EquipmentBomDetailParam equipmentBomDetailParam) {
        EquipmentBomDetail equipmentBomDetail = new EquipmentBomDetail();
        BeanUtils.copyProperties(equipmentBomDetailParam, equipmentBomDetail);

        boolean b = equipmentBomDetailServiceBiz.save(equipmentBomDetail);
        if(b){
            return successful(b);
        }else {
            return failed(IErrorCode.CODE_FAILED,"创建失败");
        }
    }

    @Override
    public R<Boolean> update(EquipmentBomDetailParam equipmentBomDetailParam) {
        EquipmentBomDetail equipmentBomDetail = new EquipmentBomDetail();
        BeanUtils.copyProperties(equipmentBomDetailParam, equipmentBomDetail);

        Boolean b = equipmentBomDetailServiceBiz.updateById(setCommonField(equipmentBomDetail));
        if(b){
            return successful(b);
        }else {
            return failed(IErrorCode.CODE_FAILED,"更新失败");
        }
    }

    @Override
    public R<Boolean> delete(IdParam idParam) {
        Assert.notNull(ApiErrorCode.INVALID_PARAMETER, idParam.getId());
        boolean b = equipmentBomDetailServiceBiz.removeById(idParam.getId());
        if(b){
            return successful(b);
        }else {
            return failed(IErrorCode.CODE_FAILED,"删除失败");
        }
    }

    @Override
    public R<EquipmentBomDetailVo> findById(IdParam idParam) {
        Assert.notNull(ApiErrorCode.INVALID_PARAMETER, idParam.getId());
        EquipmentBomDetail byId = equipmentBomDetailServiceBiz.getById(idParam.getId());
        if(byId!=null){
            EquipmentBomDetailVo equipmentBomDetailVo = EquipmentBomDetailVo.builder().build();
            BeanUtils.copyProperties(byId,equipmentBomDetailVo);
            return successful(equipmentBomDetailVo);
        }
        return failed(IErrorCode.CODE_FAILED,"查找的内容不存在");
    }

    @Override
    public R<IPage<EquipmentBomDetailVo>> findAllByEQBomId(EquipmentBomDetailQueryParam equipmentBomDetailBaseParam) {
//        QueryWrapper<EquipmentBomDetail> wrapper = new QueryWrapper<>();
//        wrapper.eq(EquipmentBomDetail.EQUIPMENT_BOM_ID, equipmentBomDetailBaseParam.getKeywords());
//        Page<EquipmentBomDetail> page = equipmentBomDetailServiceBiz.page(new Page<>(equipmentBomDetailBaseParam.getCurrent(), equipmentBomDetailBaseParam.getSize()),wrapper);
//        return successful(page);

        IPage<EquipmentBomDetailVo> iPage = equipmentBomDetailEQMapper.findAllByBomId(
                new Page<>(equipmentBomDetailBaseParam.getCurrent(), equipmentBomDetailBaseParam.getSize()),
                equipmentBomDetailBaseParam);
        return successful(iPage);
    }
}
